#include "queue.h"
#include "stdio.h"
#include "stdlib.h"

/*
    LIST_INIT(head) :对head进行初始化
	LIST_INSERT_HEAD(head, elm, field) : 从list头部插入elm
	LIST_REMOVE(elm, field) : 将elm从list中删除
	LIST_INSERT_AFTER(listelm, elm, field) : 将elm插入到listelm后面
	LIST_INSERT_BEFORE(listelm, elm, field) : 将elm插入到liistelm之前
	LIST_FOREACH(var, head, field) : 循环遍历所有节点, var为中间变量 
	LIST_EMPTY(head) : 判断list是否为空
	LIST_FIRST(head) : 获取第一个节点
	LIST_NEXT(elm, field) : 获取elm的下一个节点
*/

struct node{
    LIST_ENTRY(node) next;
    int num;
};
struct tailqNode{
    TAILQ_ENTRY(tailqNode) next;
    int num;
};

LIST_HEAD(head, node);
TAILQ_HEAD(tailq_head, tailqNode);

struct head* lhead  = NULL;

struct tailq_head *tailq_lhead = NULL;
struct tailq_head *tailq_llhead = NULL;

void list_test(void)
{
    lhead = (struct head *)malloc(sizeof(struct head));
    LIST_INIT(lhead);

    struct node *var;
    struct node *num_1 = (struct node *)malloc(sizeof(struct node));

    num_1->num = 1;

    LIST_INSERT_HEAD(lhead, num_1, next);
    LIST_FOREACH(var, lhead, next)
    {
        printf("%d->", var->num);
    }
    printf("null\r\n");

    struct node *num_2 = (struct node *)malloc(sizeof(struct node));
    num_2->num = 2;

    struct node *num_3 = (struct node *)malloc(sizeof(struct node));
    num_3->num = 3;

    LIST_INSERT_HEAD(lhead, num_2, next);
    LIST_INSERT_HEAD(lhead, num_3, next);
    LIST_FOREACH(var, lhead, next)
    {
        printf("%d->", var->num);
    }
    printf("null\r\n");

    struct node *num_4 = (struct node *)malloc(sizeof(struct node));
    num_4->num = 4;

    struct node *num_5 = (struct node *)malloc(sizeof(struct node));
    num_5->num = 5;
    LIST_INSERT_AFTER(num_3, num_4, next);
    LIST_INSERT_BEFORE(num_3, num_5, next);

    LIST_FOREACH(var, lhead, next)
    {
        printf("%d->", var->num);
    }
    printf("null\r\n");

    LIST_REMOVE(num_3,next);
    LIST_FOREACH(var, lhead, next)
    {
        printf("%d->", var->num);
    }
    printf("null\r\n");

    var = LIST_NEXT(num_4, next);
    printf("4's next is %d\n", var->num);
}


void tail_queue_test(void)
{
    tailq_lhead = (struct tailq_head *)malloc(sizeof(struct tailqNode));
    TAILQ_INIT(tailq_lhead);

    struct tailqNode* var = 0;
    struct tailqNode* num[9];

    for(int i = 0; i < 9; i ++)
    {
        num[i] = (struct tailqNode*)malloc(sizeof(struct tailqNode));
		num[i]->num = i;
    }

    TAILQ_INSERT_HEAD(tailq_lhead,  num[0], next);
    TAILQ_INSERT_HEAD(tailq_lhead,  num[1], next);
    TAILQ_INSERT_TAIL(tailq_lhead,  num[2], next);
    TAILQ_INSERT_AFTER(tailq_lhead, num[1], num[3], next);
    TAILQ_INSERT_BEFORE( num[2], num[4], next);

    TAILQ_FOREACH(var, tailq_lhead, next){
		printf("%d->", var->num);
	}
	printf("null\n");

    tailq_llhead = (struct tailqNode*)malloc(sizeof(struct head));
    TAILQ_INIT(tailq_llhead);

	TAILQ_INSERT_TAIL(tailq_llhead, num[5], next);
	TAILQ_INSERT_TAIL(tailq_llhead, num[6], next);
	TAILQ_INSERT_TAIL(tailq_llhead, num[7], next);
	TAILQ_INSERT_TAIL(tailq_llhead, num[8], next);
	TAILQ_FOREACH(var, tailq_llhead, next){
		printf("%d->", var->num);
	}
	printf("null\n");
	
	TAILQ_REMOVE(tailq_llhead, num[6], next);
	TAILQ_FOREACH(var, tailq_llhead, next){
		printf("%d->", var->num);
	}
	printf("null\n");
	
	TAILQ_FOREACH_REVERSE(var, tailq_lhead, tailq_head, next){
		printf("%d->", var->num);
	}
	printf("null\n");
		
	TAILQ_CONCAT(tailq_lhead, tailq_llhead, next);
	TAILQ_FOREACH(var, tailq_lhead, next){
		printf("%d->", var->num);
	}
	printf("null\n");

}